<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\enterpriseContact\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\enterpriseContact\model\enterpriseContact;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 企业联系逻辑层
 */
class enterpriseContactLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new enterpriseContact();
    }
    public function export($where = [])
    {
        $query = $this->search($where);
        // ->field('id,name,enterprise_id,sort,status,create_time');
        $data = $this->getAll($query);

        $file_name = '供需企业联系数据.xlsx';
        // 更新表头以匹配新的列数据
        $header = ['企业名称', '联系人', '联系电话'];
        
        $filter = [
           
        ];
        
        $data_attr = [];
        foreach($data as $value){
            $data_attr[] = [
                'enterprise_name' => $value['enterprise_name'],
                'contact_person' => $value['contact_person'],
                'contact_number' => $value['contact_number']
            ];
        }

        $writer = new OpenSpoutWriter($file_name);
        // 更新列宽以匹配新的列数
        $writer->setWidth([30, 20, 20]);
        $writer->setHeader($header);
        $writer->setData($data_attr, null, $filter);
        $file_path = $writer->returnFile();
        return response()->download($file_path, urlencode($file_name));
    }

   
}
